package com.wangsd.common.component;

import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.impl.WxPayServiceApacheHttpImpl;
import com.wangsd.common.component.extBean.*;
import org.apache.commons.lang3.StringUtils;

public class WxPayServiceExtImpl extends WxPayServiceApacheHttpImpl implements WxPayServiceExt {

    @Override
    public PaymentReceiverResponse addreceiver(PaymentReceiverRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/pay/profitsharingaddreceiver";
        String responseContent = this.post(url, request.toXML(), false);
        PaymentReceiverResponse result = PaymentReceiverResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public PaymentReceiverResponse removereceiver(PaymentReceiverRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/pay/profitsharingremovereceiver";
        String responseContent = this.post(url, request.toXML(), false);
        PaymentReceiverResponse result = PaymentReceiverResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public PaymentPostResponse profitsharing(PaymentPostRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/secapi/pay/profitsharing";
        String responseContent = this.post(url, request.toXML(), true);
        PaymentPostResponse result = PaymentPostResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public PaymentQueryResponse profitsharingquery(PaymentQueryRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/pay/profitsharingquery";
        String responseContent = this.post(url, request.toXML(), false);
        PaymentQueryResponse result = PaymentQueryResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public FacePayResponse facepay(FacePayRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/pay/facepay";
        String responseContent = this.post(url, request.toXML(), false);
        FacePayResponse result = FacePayResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public AuthinfoResponse getWxpayfaceAuthinfo(AuthinfoRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/face/get_wxpayface_authinfo";
        String responseContent = this.post(url, request.toXML(), false);
        AuthinfoResponse result = AuthinfoResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        result.setStoreId(request.getStoreId());
        return result;
    }

    @Override
    public WxPayMicropayResultExt micropayExt(WxPayMicropayRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/pay/micropay";
        String responseContent = this.post(url, request.toXML(), false);
        WxPayMicropayResultExt result = WxPayMicropayResultExt.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), false);
        return result;
    }

    @Override
    public WxPayOrderQueryResultExt queryOrderExt(WxPayOrderQueryRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());

        String url = this.getPayBaseUrl() + "/pay/orderquery";
        String responseContent = this.post(url, request.toXML(), false);
        if (StringUtils.isBlank(responseContent)) {
            throw new WxPayException("无响应结果");
        }
        WxPayOrderQueryResultExt result = WxPayOrderQueryResultExt.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositFacePayResponse depositfacepay(DepositFacePayRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/facepay";
        String responseContent = this.post(url, request.toXML(), false);
        DepositFacePayResponse result = DepositFacePayResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), false);
        return result;
    }

    @Override
    public DepositOrderQueryResponse depositorderquery(DepositOrderQueryRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/orderquery";
        String responseContent = this.post(url, request.toXML(), false);
        DepositOrderQueryResponse result = DepositOrderQueryResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositRefundResponse depositrefund(DepositRefundRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/refund";
        String responseContent = this.post(url, request.toXML(), true);
        DepositRefundResponse result = DepositRefundResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositRefundQueryResponse depositrefundquery(DepositRefundQueryRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/refundquery";
        String responseContent = this.post(url, request.toXML(), false);
        DepositRefundQueryResponse result = DepositRefundQueryResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositMicropayResponse depositmicropay(DepositMicropayRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/micropay";
        String responseContent = this.post(url, request.toXML(), false);
        DepositMicropayResponse result = DepositMicropayResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositConsumeResponse depositconsume(DepositConsumeRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/consume";
        String responseContent = this.post(url, request.toXML(), true);
        DepositConsumeResponse result = DepositConsumeResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }

    @Override
    public DepositReverseResponse depositreverse(DepositReverseRequest request) throws WxPayException {
        request.checkAndSign(this.getConfig());
        String url = this.getPayBaseUrl() + "/deposit/reverse";
        String responseContent = this.post(url, request.toXML(), true);
        DepositReverseResponse result = DepositReverseResponse.fromXML(responseContent);
        result.checkResult(this, request.getSignType(), true);
        return result;
    }
}
